<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品方案" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择产品方案" clearable>
          <el-option
            v-for="dict in dict.type.paic_fti_product_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="policyId" />
      <el-table-column label="产品方案" align="center" prop="productType" />
      <el-table-column label="发票号码" align="center" prop="invoiceNumber" />
      <el-table-column label="运单号码" align="center" prop="waybillNumber" />
      <el-table-column label="车牌号" align="center" prop="licensePlateNumber" />
      <el-table-column label="保额" align="center" prop="insuranceAmount" />
      <el-table-column label="保费" align="center" prop="premium" />
      <el-table-column label="保单号" align="center" prop="policyNo" />
      <el-table-column label="保单状态" align="center" prop="policyStatus" />
      <el-table-column label="被保人" align="center" prop="customerName" />
      <el-table-column label="投保时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

<!--          <a style="color: #189dfe; margin-left: 20px" :href="scope.row.policyFileUrl" target="_self" class="buttonText">下载</a>-->

          <el-button
            v-show="scope.row.policyStatus == '投保成功'"
            size="mini"
            type="text"
            @click="resource(scope.row.policyFileUrl)"
            v-hasPermi="['core:rate:edit']"
          >下载保单</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { policyList  } from "@/api/paic/ftipolicy";
import { resource } from "@/plugins/download"
import axios from 'axios'
import { getToken } from '@/utils/auth'
import { blobValidate } from "@/utils/ruoyi";
import { saveAs } from 'file-saver'


const baseURL = process.env.VUE_APP_BASE_API
export default {
  name: "paicftipolicy",
  dicts: ['paic_fti_product_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平安货运险费率表格数据
      rateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,


      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询平安货运险费率列表 */
    getList() {
      this.loading = true;
      policyList(this.queryParams).then(response => {
        this.rateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productType: null,
        mainInsurance: null,
        rate: null,
        minPremium: null,
        createTime: null,
        updateTime: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    resource(resource) {
      debugger
      resource = resource.replace(baseURL,"");
      var url = baseURL + "/common/download/resource?resource=" + encodeURIComponent(resource);
      axios({
        method: 'get',
        url: url,
        responseType: 'blob',
        headers: { 'Authorization': 'Bearer ' + getToken() }
      }).then((res) => {
        const isBlob = blobValidate(res.data);
        if (isBlob) {
          const blob = new Blob([res.data])
          this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
        } else {
          this.printErrMsg(res.data);
        }
      })
    },

    saveAs(text, name, opts) {
      saveAs(text, name, opts);
    },



  }
};
</script>
